package com.cyanite.playlv2;

import com.cyanite.playlv2.preferences.Preferences;
import com.cyanite.playlv2.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * Can be presented as a dialog theme, collects data from the user for a checkin or a
 * shout. The playlv2api is the same for checkins and shouts. A checkin should just
 * contain a venue id.
 *
 * After the user has entered their data, this activity will finish itself and call
 * either CheckinExecuteActivity or ShoutExecuteActivity. The only real difference
 * between them is what's displayed at the conclusion of the execution.
 *
 * If doing a checkin, the user can also skip this activity and do a 'quick' checkin
 * by launching CheckinExecuteActivity directly. This will just use their saved preferences
 * to checkin at the specified venue, no optional shout message will be attached to
 * the checkin.
 *
 * This dialog allows the user to supply the following information:
 *
 * <ul>
 *  <li>Tell my Friends [yes|no]</li>
 *  <li>EditField for freeform shout text.</li>
 * </ul>
 *
 */
public class CheckinOrShoutGatherInfoActivity extends Activity {

    public static final String INTENT_EXTRA_IS_CHECKIN = PlayLv2.PACKAGE_NAME
            + ".CheckinOrShoutGatherInfoActivity.INTENT_EXTRA_IS_CHECKIN";
    public static final String INTENT_EXTRA_IS_SHOUT = PlayLv2.PACKAGE_NAME
            + ".CheckinOrShoutGatherInfoActivity.INTENT_EXTRA_IS_SHOUT";
    public static final String INTENT_EXTRA_VENUE_ID = PlayLv2.PACKAGE_NAME
            + ".CheckinOrShoutGatherInfoActivity.INTENT_EXTRA_VENUE_ID";
    public static final String INTENT_EXTRA_VENUE_NAME = PlayLv2.PACKAGE_NAME
            + ".CheckinOrShoutGatherInfoActivity.INTENT_EXTRA_VENUE_NAME";
    public static final String INTENT_EXTRA_TEXT_PREPOPULATE = PlayLv2.PACKAGE_NAME
            + ".CheckinOrShoutGatherInfoActivity.INTENT_EXTRA_TEXT_PREPOPULATE";

    private StateHolder mStateHolder;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin_or_shout_gather_info_activity);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(PlayLv2.INTENT_ACTION_LOGGED_OUT));

        Object retained = getLastNonConfigurationInstance();
        if (retained != null && retained instanceof StateHolder) {
            mStateHolder = (StateHolder) retained;
        } else {

            if (getIntent().getExtras() != null) {
                if (getIntent().getBooleanExtra(INTENT_EXTRA_IS_CHECKIN, false)) {
                    // If a checkin, we require venue id and name.
                    String venueId = null;
                    if (getIntent().getExtras().containsKey(INTENT_EXTRA_VENUE_ID)) {
                        venueId = getIntent().getExtras().getString(INTENT_EXTRA_VENUE_ID);
                    } else {
                        finish();
                        return;
                    }

                    String venueName = null;
                    if (getIntent().getExtras().containsKey(INTENT_EXTRA_VENUE_NAME)) {
                        venueName = getIntent().getExtras().getString(INTENT_EXTRA_VENUE_NAME);
                    } else {
                        finish();
                        return;
                    }

                    mStateHolder = new StateHolder(true, venueId, venueName);
                }
                else if (getIntent().getBooleanExtra(INTENT_EXTRA_IS_SHOUT, false)) {
                    // If a shout, we don't require anything at all.
                    mStateHolder = new StateHolder(false, null, null);
                }
                else {
                    finish();
                    return;
                }

                if (getIntent().hasExtra(INTENT_EXTRA_TEXT_PREPOPULATE)) {
                    EditText editShout = (EditText)findViewById(R.id.editTextShout);
                    editShout.setText(getIntent().getStringExtra(INTENT_EXTRA_TEXT_PREPOPULATE));
                }
            }
            else {
                finish();
                return;
            }
        }

        ensureUi();
    }

    @Override
    public void onPause() {
        super.onPause();

        if (isFinishing()) {
            unregisterReceiver(mLoggedOutReceiver);
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mStateHolder;
    }

    private void ensureUi() {
        if (mStateHolder.getIsCheckin()) {
            setTitle(getResources().getString(R.string.checkin_title_checking_in,
                    mStateHolder.getVenueName()));
        } else {
            setTitle(getResources().getString(R.string.shout_action_label));
        }

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        CheckBox cbTellFriends = (CheckBox)findViewById(R.id.checkboxTellFriends);
        cbTellFriends.setChecked(settings.getBoolean(Preferences.PREFERENCE_SHARE_CHECKIN, true));

        Button btnCheckin = (Button)findViewById(R.id.btnCheckin);
        if (mStateHolder.getIsCheckin()) {
            btnCheckin.setText(getResources().getString(R.string.checkin_action_label));
        } else {
            btnCheckin.setText(getResources().getString(R.string.shout_action_label));
        }
        btnCheckin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checkin();
            }
        });
    }

    private void checkin() {
        CheckBox cbTellFriends = (CheckBox)findViewById(R.id.checkboxTellFriends);
        EditText editShout = (EditText)findViewById(R.id.editTextShout);

        // After we start the activity, we don't have to stick around any longer.
        // We want to forward the resultCode of CheckinExecuteActivity to our
        // caller though, so add the FLAG_ACTIVITY_FORWARD_RESULT on the intent.
        Intent intent = new Intent();
        if (mStateHolder.getIsCheckin()) {
            intent.setClass(this, CheckinExecuteActivity.class);
            intent.putExtra(CheckinExecuteActivity.INTENT_EXTRA_VENUE_ID, mStateHolder.getVenueId());
            intent.putExtra(CheckinExecuteActivity.INTENT_EXTRA_SHOUT, editShout.getText().toString());
            intent.putExtra(CheckinExecuteActivity.INTENT_EXTRA_TELL_FRIENDS, cbTellFriends.isChecked());
        }
        else {
            intent.setClass(this, ShoutExecuteActivity.class);
            intent.putExtra(ShoutExecuteActivity.INTENT_EXTRA_SHOUT, editShout.getText().toString());
            intent.putExtra(ShoutExecuteActivity.INTENT_EXTRA_TELL_FRIENDS, cbTellFriends.isChecked());
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        startActivity(intent);
        finish();
    }

    private static class StateHolder {
        private boolean mIsCheckin; // either a checkin, or a shout.
        private String mVenueId;
        private String mVenueName;

        public StateHolder(boolean isCheckin, String venueId, String venueName) {
            mIsCheckin = isCheckin;
            mVenueId = venueId;
            mVenueName = venueName;
        }

        public boolean getIsCheckin() {
            return mIsCheckin;
        }

        public String getVenueId() {
            return mVenueId;
        }

        public String getVenueName() {
            return mVenueName;
        }
    }
}
